﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pixysoft.BarCode
{
    class EAN8
    {
        private string[] EAN_CodeA = { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011" };

        private string[] EAN_CodeC = { "1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100" };

        private static volatile EAN8 instance;

        private static object syncRoot = new Object();

        public static EAN8 Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new EAN8();
                        }
                    }
                }
                return instance;
            }
        }

        private string CheckDigit(string barcode)
        {
            string _barcode = barcode;

            if (_barcode.Length == 7)
            {
                //calculate the checksum digit
                int even = 0;
                int odd = 0;

                //odd
                for (int i = 0; i <= 6; i += 2)
                {
                    odd += Int32.Parse(_barcode.Substring(i, 1)) * 3;
                }//for

                //even
                for (int i = 1; i <= 5; i += 2)
                {
                    even += Int32.Parse(_barcode.Substring(i, 1));
                }//for

                int total = even + odd;
                int checksum = total % 10;
                checksum = 10 - checksum;
                if (checksum == 10)
                    checksum = 0;

                //add the checksum to the end of the 
                _barcode += checksum.ToString();
            }

            return _barcode;
        }

        public string GetBarCode(string barcode)
        {
            //check length
            if (barcode.Length != 8 && barcode.Length != 7) throw new Exception("EEAN8-1: Invalid data length. (7 or 8 numbers only)");

            //check numeric only
            if (!Pixysoft.NumberCoder.Instance.IsInt64(barcode)) throw new Exception("EEAN8-2: Numeric only.");

            //encode the data
            string result = "101";

            //first half (Encoded using left hand / odd parity)
            for (int i = 0; i < barcode.Length / 2; i++)
            {
                result += EAN_CodeA[Int32.Parse(barcode[i].ToString())];
            }//for

            //center guard bars
            result += "01010";

            //second half (Encoded using right hand / even parity)
            for (int i = barcode.Length / 2; i < barcode.Length; i++)
            {
                result += EAN_CodeC[Int32.Parse(barcode[i].ToString())];
            }//for

            result += "101";

            return result;
        }


    }
}
